<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试页面</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
        }

        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
        }

        .test-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 15px;
            color: #333;
        }

        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            font-size: 14px;
            cursor: pointer;
            margin-right: 10px;
            margin-bottom: 10px;
        }

        .btn-primary {
            background: #007bff;
            color: white;
        }

        .btn-success {
            background: #28a745;
            color: white;
        }

        .btn-warning {
            background: #ffc107;
            color: #212529;
        }

        .result {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
            font-family: monospace;
            font-size: 12px;
            white-space: pre-wrap;
            max-height: 200px;
            overflow-y: auto;
        }

        .success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .info {
            background: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>API测试页面</h1>
        <p>用于测试后端API路径是否正确</p>

        <!-- 用户API测试 -->
        <div class="test-section">
            <div class="test-title">用户API测试</div>
            <button class="btn btn-primary" onclick="testUserSignUp()">测试用户注册</button>
            <button class="btn btn-success" onclick="testUserSignIn()">测试用户登录</button>
            <div id="userResult" class="result"></div>
        </div>

        <!-- 社区API测试 -->
        <div class="test-section">
            <div class="test-title">社区API测试</div>
            <button class="btn btn-primary" onclick="testCreatePost()">测试创建帖子</button>
            <button class="btn btn-success" onclick="testGetPosts()">测试获取帖子</button>
            <button class="btn btn-warning" onclick="testCreateComment()">测试创建评论</button>
            <button class="btn btn-warning" onclick="testGetComments()">测试获取评论</button>
            <div id="communityResult" class="result"></div>
        </div>

        <!-- 个人信息API测试 -->
        <div class="test-section">
            <div class="test-title">个人信息API测试</div>
            <button class="btn btn-primary" onclick="testSaveProfile()">测试保存个人信息</button>
            <button class="btn btn-success" onclick="testGetProfile()">测试获取个人信息</button>
            <div id="profileResult" class="result"></div>
        </div>
    </div>

    <script>
        // 测试用户注册
        function testUserSignUp() {
            const resultDiv = document.getElementById('userResult');
            resultDiv.textContent = '正在测试用户注册...';
            resultDiv.className = 'result info';

            fetch('/api/user/signUp', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    username: 'testuser' + Date.now(),
                    password: '123456',
                    confirmPassword: '123456'
                })
            })
                .then(response => response.json())
                .then(data => {
                    resultDiv.textContent = '用户注册结果:\n' + JSON.stringify(data, null, 2);
                    resultDiv.className = 'result ' + (data.success ? 'success' : 'error');
                })
                .catch(error => {
                    resultDiv.textContent = '用户注册失败:\n' + error.message;
                    resultDiv.className = 'result error';
                });
        }

        // 测试用户登录
        function testUserSignIn() {
            const resultDiv = document.getElementById('userResult');
            resultDiv.textContent = '正在测试用户登录...';
            resultDiv.className = 'result info';

            fetch('/api/user/signIn', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    username: 'testuser',
                    password: '123456'
                })
            })
                .then(response => response.json())
                .then(data => {
                    resultDiv.textContent = '用户登录结果:\n' + JSON.stringify(data, null, 2);
                    resultDiv.className = 'result ' + (data.success ? 'success' : 'error');
                })
                .catch(error => {
                    resultDiv.textContent = '用户登录失败:\n' + error.message;
                    resultDiv.className = 'result error';
                });
        }

        // 测试创建帖子
        function testCreatePost() {
            const resultDiv = document.getElementById('communityResult');
            resultDiv.textContent = '正在测试创建帖子...';
            resultDiv.className = 'result info';

            fetch('/api/community-posts', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    userId: 1,
                    title: '测试帖子',
                    content: '这是一个测试帖子的内容'
                })
            })
                .then(response => response.json())
                .then(data => {
                    resultDiv.textContent = '创建帖子结果:\n' + JSON.stringify(data, null, 2);
                    resultDiv.className = 'result ' + (data.success ? 'success' : 'error');
                })
                .catch(error => {
                    resultDiv.textContent = '创建帖子失败:\n' + error.message;
                    resultDiv.className = 'result error';
                });
        }

        // 测试获取帖子
        function testGetPosts() {
            const resultDiv = document.getElementById('communityResult');
            resultDiv.textContent = '正在测试获取帖子...';
            resultDiv.className = 'result info';

            fetch('/api/community-posts')
                .then(response => response.json())
                .then(data => {
                    resultDiv.textContent = '获取帖子结果:\n' + JSON.stringify(data, null, 2);
                    resultDiv.className = 'result ' + (data.success ? 'success' : 'error');
                })
                .catch(error => {
                    resultDiv.textContent = '获取帖子失败:\n' + error.message;
                    resultDiv.className = 'result error';
                });
        }

        // 测试创建评论
        function testCreateComment() {
            const resultDiv = document.getElementById('communityResult');
            resultDiv.textContent = '正在测试创建评论...';
            resultDiv.className = 'result info';

            fetch('/api/post-comments', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    postId: 1,
                    userId: 1,
                    content: '这是一个测试评论'
                })
            })
                .then(response => response.json())
                .then(data => {
                    resultDiv.textContent = '创建评论结果:\n' + JSON.stringify(data, null, 2);
                    resultDiv.className = 'result ' + (data.success ? 'success' : 'error');
                })
                .catch(error => {
                    resultDiv.textContent = '创建评论失败:\n' + error.message;
                    resultDiv.className = 'result error';
                });
        }

        // 测试获取评论
        function testGetComments() {
            const resultDiv = document.getElementById('communityResult');
            resultDiv.textContent = '正在测试获取评论...';
            resultDiv.className = 'result info';

            fetch('/api/post-comments/post/1')
                .then(response => response.json())
                .then(data => {
                    resultDiv.textContent = '获取评论结果:\n' + JSON.stringify(data, null, 2);
                    resultDiv.className = 'result ' + (data.success ? 'success' : 'error');
                })
                .catch(error => {
                    resultDiv.textContent = '获取评论失败:\n' + error.message;
                    resultDiv.className = 'result error';
                });
        }

        // 测试保存个人信息
        function testSaveProfile() {
            const resultDiv = document.getElementById('profileResult');
            resultDiv.textContent = '正在测试保存个人信息...';
            resultDiv.className = 'result info';

            fetch('/api/user-profile/save', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    userId: 1,
                    height: 175.0,
                    weight: 70.0,
                    gender: '男',
                    birthday: '1990-01-01'
                })
            })
                .then(response => response.json())
                .then(data => {
                    resultDiv.textContent = '保存个人信息结果:\n' + JSON.stringify(data, null, 2);
                    resultDiv.className = 'result ' + (data.success ? 'success' : 'error');
                })
                .catch(error => {
                    resultDiv.textContent = '保存个人信息失败:\n' + error.message;
                    resultDiv.className = 'result error';
                });
        }

        // 测试获取个人信息
        function testGetProfile() {
            const resultDiv = document.getElementById('profileResult');
            resultDiv.textContent = '正在测试获取个人信息...';
            resultDiv.className = 'result info';

            fetch('/api/user-profile/1')
                .then(response => response.json())
                .then(data => {
                    resultDiv.textContent = '获取个人信息结果:\n' + JSON.stringify(data, null, 2);
                    resultDiv.className = 'result ' + (data.success ? 'success' : 'error');
                })
                .catch(error => {
                    resultDiv.textContent = '获取个人信息失败:\n' + error.message;
                    resultDiv.className = 'result error';
                });
        }
    </script>
</body>

</html>